package com.bensonbasement.snorflux.server.world;

import java.util.LinkedList;
import java.util.List;

import com.bensonbasement.snorflux.memory.BigEntityInfo;
import com.bensonbasement.snorflux.memory.EntityInfo;
import com.bensonbasement.snorflux.server.world.prototypes.BigEntityPrototype;
import com.bensonbasement.snorflux.server.world.prototypes.EntityPrototype;

public class BigEntity extends Entity implements ISmallEntityHolder {
	
	private InventoryHelper inventoryHelper = new InventoryHelper();
	private IBigEntityHolder location = null;
	
	public BigEntity(World world, BigEntityPrototype prototype) {
		super(world,prototype);
	}
	
	@Override
	protected BigEntityPrototype getPrototype() {
		return (BigEntityPrototype) super.getPrototype();
	}
	
	@Override
	public void addSmallEntity(SmallEntity entity) {
		inventoryHelper.addSmallEntity(entity);
	}

	@Override
	public void removeSmallEntity(SmallEntity entity) {
		inventoryHelper.removeSmallEntity(entity);
	}

	@Override
	public List<SmallEntity> getSmallEntities() {
		return inventoryHelper.getSmallEntities();
	}

	public void setLocation(IBigEntityHolder newLocation) {
		this.setLocationHelper(newLocation);
	}

	public IBigEntityHolder getLocation() {
		return location;
	}

	public boolean isVisionBlocker() {
		return ((BigEntityPrototype)this.getPrototype()).findVisionBlocker();
	}

	@Override
	public BigEntityInfo render(IViewer viewer) {
		// TODO: Update this when the Jython attribute stuff is created.
		if (viewer.canSeeInLocation(this.getLocation())) {
			BigEntityInfo retVal = new BigEntityInfo(this.getWorldElementID(),this.getGlyph(),this.getName(),this.getLocation().getWorldElementID());
			return retVal;
		}
		else {
			return null;
		}
	}

	@Override
	protected void addSelfTo(ILocation location) {
		IBigEntityHolder loc = (IBigEntityHolder) location;
		assert loc.getBigEntity() == null;
		loc.setBigEntity(this);
	}

	@Override
	protected void removeSelfFrom(ILocation location) {
		IBigEntityHolder loc = (IBigEntityHolder) location;
		assert loc.getBigEntity() == this;
		loc.setBigEntity(null);
	}

	@Override
	protected void actuallySetLocation(ILocation location) {
		IBigEntityHolder loc = (IBigEntityHolder) location;
		this.location = loc;
	}
	
	@Override
	public List<WorldElement> getAllWorldElements() {
		LinkedList<WorldElement> retVal = new LinkedList<WorldElement>();
		retVal.add(this);
		for (SmallEntity entity : this.inventoryHelper.getSmallEntities()) {
			retVal.addAll(entity.getAllWorldElements());
		}
		return retVal;
	}
}
